﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WindowsFormsApplication1;


namespace PO_2011
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private Pnm p;
        Stream myStream;
        String fileName = "";
        Form2 HistogramWindow;
        BrightnessWindow BrightnessWin;
        ContrastWindow ContrastWin;
        GammaWindow GammaWin;


        private void loadFIleToolStripMenuItem_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "PBM files (*.pbm)|*.pbm|PGM files (*.pgm)|*.pgm|PPM files (*.ppm)|*.ppm";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //Przekazanie do klasy 
                    myStream = openFileDialog1.OpenFile();
                    p = new Pnm();
                    p.LoadFile(myStream);

                    Console.WriteLine(p.getTypeOfFile());
                    Console.WriteLine(p.getComment());
                    Console.WriteLine(p.getWidth());
                    Console.WriteLine(p.getHeight());
                    Console.WriteLine(p.getMaxValue());

                    //pobranie danych do obrazu
                    int height = p.GetHeight;
                    int width = p.GetWidth;
                    int typeOffile = p.getTypeOfFile();

                    pictureBox1.Size = new System.Drawing.Size(width, height);
                    //pictureBox1.Image = p.Bitmap;
                    pictureBox1.Image = p.getBitmap();
                    
                    

                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String typName = "";
            SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            SaveFileDialog1.InitialDirectory = "c:\\";
            try
            {
                int type = p.getTypeOfFile();

                switch (type)
                {
                    case 1: typName = "pbm"; ; break;
                    case 2: typName = "pgm"; ; break;
                    case 3: typName = "ppm"; ; break;
                    case 4: typName = "pbm"; ; break;
                    case 5: typName = "pgm"; ; break;
                    case 6: typName = "ppm"; ; break;
                    default: MessageBox.Show("Wrong type of file! (switch)!"); break;
                }
                SaveFileDialog1.Filter = typName + " files (*." + typName + ")|*." + typName;
                SaveFileDialog1.RestoreDirectory = false;

                if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    fileName = SaveFileDialog1.FileName;//pełna ścieżka którą wybrałem
                }

                p.ExportFile(myStream, fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded. Original error: " + ex.Message);
            }
        }

        private void histogramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try 
            {
                HistogramWindow = new Form2(p);
                HistogramWindow.refToMainWindow = this;
                HistogramWindow.pictureBoxHistogram.Image = p.Histogram;
                HistogramWindow.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded, there is no image to compute histograms. Original error: " + ex.Message);
            }
            
        }

        private void aboutThisProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox aboutBox = new AboutBox();
            aboutBox.Show();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap b = p.CopyBitmap;
                p.Bitmap = b;
                pictureBox1.Image = p.Bitmap;
            }
            catch (Exception ex)
            { 
                //wiem, wiem .. ale to było na szybkiego..
                Console.WriteLine("Error: Nie można cofnąć " + ex.Message);

            }
        }

        private void strechToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                p.ComputeHistogramR();
                p.ComputeCummulatedHistogram();
                p.Strech();

                p.ComputeHistogramG();
                p.ComputeCummulatedHistogram();
                p.Strech();

                p.ComputeHistogramB();
                p.ComputeCummulatedHistogram();
                p.Strech();

                pictureBox1.Image = p.Bitmap;

                if (HistogramWindow != null && HistogramWindow.Visible == true)
                    HistogramWindow.strechHistogramButton_Click(sender, e);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded, there is no image to strech. Original error: " + ex.Message);
            }
            
        }

        private void equalizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
            p.ComputeHistogramR();
            p.ComputeCummulatedHistogram();
            p.Equalize();

            p.ComputeHistogramG();
            p.ComputeCummulatedHistogram();
            p.Equalize();

            p.ComputeHistogramB();
            p.ComputeCummulatedHistogram();
            p.Equalize();

            pictureBox1.Image = p.Bitmap;

            if (HistogramWindow != null && HistogramWindow.Visible == true)
                HistogramWindow.equalizationButton_Click(sender, e);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded, there is no image to equalize. Original error: " + ex.Message);
            }
        }

       

        private void rGBToGrayScaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //zrobic to co histogram na L
            p.RGBtoGrayScale();
            this.pictureBox1.Image = p.Bitmap;
        }

        private void rGBToCMYKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            p.RGBtoCMYK();
            this.pictureBox1.Image = p.Bitmap;
        }

        private void brightnessToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                Bitmap copyOfactualBitmap = p.Bitmap;
                BrightnessWin = new BrightnessWindow(p, copyOfactualBitmap);
                BrightnessWin.refToMainWindow = this;
                BrightnessWin.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded. Original error: " + ex.Message);
            }


        }

        private void contrastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap copyOfactualBitmap = p.Bitmap;
                ContrastWin = new ContrastWindow(p, copyOfactualBitmap);
                ContrastWin.refToMainWindow = this;
                ContrastWin.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded. Original error: " + ex.Message);
            }


        }

        private void gammaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap copyOfactualBitmap = p.Bitmap;
                GammaWin = new GammaWindow(p, copyOfactualBitmap);
                GammaWin.refToMainWindow = this;
                GammaWin.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: No file loaded. Original error: " + ex.Message);
            }
        }

        private void RozmycieGJ_Click(object sender, EventArgs e)
        {
            try
            {
                RozmycieJG FiltrOgolny = new RozmycieJG(p);
                FiltrOgolny.refToMainWindow = this;
                FiltrOgolny.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Original error: " + ex.Message);
            }

        }
        private void Ogolny_Click(object sender, EventArgs e)
        {
            try
            {
                FiltrOgolny FiltrOgolny = new FiltrOgolny(p);
                FiltrOgolny.refToMainWindow = this;
                FiltrOgolny.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Original error: " + ex.Message);
            }

        }


        private void Mediana_Click(object sender, EventArgs e)
        {
            try
            {
                Mediana Mediana = new Mediana(p);
                Mediana.refToMainWindow = this;
                Mediana.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Original error: " + ex.Message);
            }
        }

        private void morfologiaMatematycznaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                MorfologiaMatemetyczna Mediana = new MorfologiaMatemetyczna(p);
                Mediana.refToMainWindow = this;
                Mediana.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Original error: " + ex.Message);
            }
        }

        private void binarizationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Binarization B = new Binarization(p);
                B.refToMainWindow = this;
                B.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Original error: " + ex.Message);
            }
        }

        private void binarizationAlgorythmsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                BinarizationAlgorythms B = new BinarizationAlgorythms(p);
                B.refToMainWindow = this;
                B.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(" Original error: " + ex.Message);
            }
        }



        }
    }

